package com.xhwl.centiotdata.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import com.xhwl.common.query.cent.device.DeviceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: lianghz
 * @Description: 设备中台接口封装
 * @Date: Create in 17:02 2021/4/22
 */
@Service
public class DeviceServiceImpl {

    @Autowired
    private IDeviceCentService deviceCentService;

    public List<DeviceDTO> findDevice(Integer projectId, String iotDeviceId) {
        return this.findDevice(null, projectId, null, null, null, iotDeviceId, null);
    }

    public List<DeviceDTO> findDevice(Short industryId, Integer projectId, Integer categoryId, List<Integer> deviceIdList) {
        return this.findDevice(industryId, projectId, categoryId, null, null, null, deviceIdList);
    }

    /**
     * 查询设备
     */
    public List<DeviceDTO> findDevice(Short industryId, Integer projectId, Integer categoryId, Integer deviceTypeId, String thirdDeviceId, String iotDeviceId, List<Integer> deviceIdList) {
        DeviceQuery deviceQuery = new DeviceQuery();
        deviceQuery.setProjectId(projectId);
        deviceQuery.setCategoryIdList(categoryId == null ? null : Arrays.asList(categoryId));
        deviceQuery.setDeviceTypeId(deviceTypeId);
        deviceQuery.setThirdDeviceId(thirdDeviceId);
        deviceQuery.setIotDeviceId(iotDeviceId);
        deviceQuery.setIdList(deviceIdList);
        List<DeviceDTO> deviceDTOS = deviceCentService.listDevice(industryId, deviceQuery);
        if (CollectionUtils.isEmpty(deviceDTOS)) {
            throw new BusinessException("未找到对应设备");
        }
        return deviceDTOS;
    }

    public DeviceDTO findOneDevice(Short industryId, Integer projectId, Integer categoryId, List<Integer> deviceIdList) {
        return this.findOneDevice(industryId, projectId, categoryId, null, null, null, deviceIdList);
    }

    public DeviceDTO findOneDevice(Short industryId, Integer projectId, Integer categoryId, Integer deviceTypeId, String thirdDeviceId, String iotDeviceId, List<Integer> deviceIdList) {
        DevicePageQuery deviceQuery = new DevicePageQuery();
        deviceQuery.setProjectId(projectId);
        deviceQuery.setCategoryIdList(categoryId == null ? null : Arrays.asList(categoryId));
        deviceQuery.setDeviceTypeId(deviceTypeId);
        deviceQuery.setThirdDeviceId(thirdDeviceId);
        deviceQuery.setIotDeviceId(iotDeviceId);
        deviceQuery.setIdList(deviceIdList);
        deviceQuery.setSize(1L);
        deviceQuery.setCurrent(1L);
        Page<DeviceDTO> deviceDTOS = deviceCentService.pageDevice(industryId, deviceQuery);
        if (CollectionUtils.isEmpty(deviceDTOS.getRecords())) {
            throw new BusinessException("未找到对应设备");
        }
        return deviceDTOS.getRecords().get(0);
    }

}